package com.le.tester.book.concurrentjavafight.l_11_4;

import org.junit.runner.notification.RunListener;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * createTime：2023/2/4 9:47
 * description：反例，将一个锁不必要的持有过长的时间
 */
@RunListener.ThreadSafe
public class AttributeStore {

    private final Map<String, String> attributeMap = new HashMap<>();

    //直接在方法上加上同步锁,那么这里想要的效果是缩小锁的范围（see betterAttributeStore）
    public synchronized boolean useLocationMatches(String name, String regexp) {
        String key = "users." + name + ".location";
        String location = attributeMap.get(key);
        if (location == null) {
            return false;
        } else {
            return Pattern.matches(regexp, location);
        }
    }


}
